import { useTmpiniaStore } from "@/tmui/tool/lib/tmpinia";
import { computed, ref } from "vue";
import useTheme from "@/tmui/tool/useFun/useTheme";

export const LOCAL_SAVE_PAGE_TYPE_KEY = "PAGE_TYPE";
export const THEME_COLOR_OWNER = "#1678FF";
export const THEME_COLOR_DRIVER = "#FF6600";

export const currentPageType = ref(getPageType());

//用于获取跟随应用设置的动态主题颜色
export function useFollowTheme() {
  const store = useTmpiniaStore();
  // 设置响应式全局组件库配置表。
  const tmCfg = computed(() => store.tmStore);
  const { theme } = useTheme(
    computed(() => {
      return { followTheme: true };
    }),
    tmCfg,
  );
  return {
    followThemeConfig: theme(),
  };
}

export function setOwnerTheme() {
  const store = useTmpiniaStore();
  store.setTmVuetifyAddTheme("owner", THEME_COLOR_OWNER, true);
}

export function setDriverTheme() {
  const store = useTmpiniaStore();
  store.setTmVuetifyAddTheme("driver", THEME_COLOR_DRIVER, true);
}

export function getPageType() {
  return (
    (uni.getStorageSync(LOCAL_SAVE_PAGE_TYPE_KEY) as "owner" | "driver") ||
    "owner"
  );
}

export function setPageType(type: "owner" | "driver" = "owner") {
  uni.setStorageSync(LOCAL_SAVE_PAGE_TYPE_KEY, type);
  currentPageType.value = type;
  if (type === "owner") {
    setOwnerTheme();
  } else {
    setDriverTheme();
  }
}
